//
//  FPMyViewController.m
//  FilmParadise
//  700 -702
//  Created by lanou3g on 15/7/13.
//  Copyright (c) 2015年 DLYgroup. All rights reserved.
//

#import "FPMyViewController.h"
#import "FPMyTableFootView.h"
#import "FPMyTableHeadView.h"
#import "FPLoginViewController.h"
#import "FPRegisterViewController.h"
#import "FPUserModel.h"
#import "FPMyCollectMovieViewController.h"
#import "FPMyWebCollectController.h"
#import "FPAlertViewShow.h"
@interface FPMyViewController ()<UITableViewDelegate, UITableViewDataSource,FPMyTableHeadViewDelegate, UIAlertViewDelegate>
@property (nonatomic , strong)UITableView *tableView;
@property (nonatomic , strong)UIView *statusBarView;
@end

@implementation FPMyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"cgClear"] forBarMetrics:UIBarMetricsDefault];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;

    [self hideNavigationBlackLine];
}

- (void)hideNavigationBlackLine
{
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kImageH(30))];
    label.textAlignment = 1;
    label.textColor = [UIColor whiteColor];
    label.text = @"用户";
    label.font = [UIFont fontWithName:@"Zapfino" size:24];
    self.navigationItem.titleView = label;
    self.navigationController.navigationBar.backgroundColor = [UIColor colorWithRed:0.180 green:0.596 blue:1.000 alpha:1.000];
    self.statusBarView.frame =CGRectMake(0, 0, kScreenWidth  , 20);
    _statusBarView.backgroundColor = [UIColor colorWithRed:0.180 green:0.596 blue:1.000 alpha:1.000];

    if ([self.navigationController.navigationBar respondsToSelector:@selector( setBackgroundImage:forBarMetrics:)]){
        NSArray *list=self.navigationController.navigationBar.subviews;
        for (id obj in list) {
            if ([obj isKindOfClass:[UIImageView class]]) {
                UIImageView *imageView=(UIImageView *)obj;
                NSArray *list2=imageView.subviews;
                for (id obj2 in list2) {
                    if ([obj2 isKindOfClass:[UIImageView class]]) {
                        UIImageView *imageView2=(UIImageView *)obj2;
                        imageView2.hidden=YES;
                    }
                }
            }
        }
    }
}
- (void)exitbtn
{
    [FPUserModel ShareInstance].userName = nil;
    [FPUserModel ShareInstance].userPhone = nil;
    [FPUserModel ShareInstance].userPwd = nil;
    [FPUserModel ShareInstance].userImage = nil;
    [((UIButton *)[self.view viewWithTag:700]) setBackgroundImage:[UIImage imageNamed:@"person"] forState:UIControlStateNormal];
    ((UIButton *)[self.view viewWithTag:701]).hidden = NO;
    ((UIButton *)[self.view viewWithTag:702]).hidden = NO;
    ((UILabel *)[self.view viewWithTag:705]).hidden = YES;
}

- (void)TableHeadView:(FPMyTableHeadView *)tableHeadView didClicButton:(UIButton *)button {
    switch (button.tag)
    {
        case 700:
            NSLog(@"personbutton");
            break;
        case 701:
        {
            NSLog(@"登录");
            FPLoginViewController *loginViewVc = [[FPLoginViewController alloc]init];
                [self.navigationController pushViewController:loginViewVc animated:YES];
            break;
        }
        case 702:
        {
            
            NSLog(@"注册");
            FPRegisterViewController *registerVC = [[FPRegisterViewController alloc]init];
            [self.navigationController pushViewController:registerVC animated:YES];
        }
            break;
        default:
            break;
    }
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    FPMyTableFootView *footV = [[FPMyTableFootView alloc]init];
    footV.exitBtn.userInteractionEnabled = YES;
    footV.userInteractionEnabled = YES;
    [footV.exitBtn addTarget:self action:@selector(exitbtn) forControlEvents:UIControlEventTouchUpInside];
    return footV;
    
    
}


- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"cgClear"] forBarMetrics:UIBarMetricsDefault];
      if ([FPUserModel ShareInstance].userName) {
        UIButton *perButton = (UIButton *)[self.view viewWithTag:700];
        [perButton setBackgroundImage:[FPUserModel ShareInstance].userImage forState:UIControlStateNormal];
        ((UIButton *)[self.view viewWithTag:701]).hidden = YES;
        ((UIButton *)[self.view viewWithTag:702]).hidden = YES;
        ((UILabel *)[self.view viewWithTag:705]).hidden = NO;
        ((UILabel *)[self.view viewWithTag:705]).text = [FPUserModel ShareInstance].userName;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return kImageH(90);
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 4;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *ID = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
    }
    if (indexPath.row == 0) {
        cell.textLabel.text = @"我的电影收藏";
    }
    else if (indexPath.row == 1)
    {
        cell.textLabel.text = @"网页收藏";
    }
    else if (indexPath.row == 2)
    {
        cell.textLabel.text = @"清空缓存";
    }
    else if (indexPath.row == 3)
    {
        cell.textLabel.text = @"当前版本: 1.0";
    }
    
    
    return cell;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    FPMyTableHeadView *headV = [[FPMyTableHeadView alloc]init];
    headV.userName.tag = 705;
    headV.delegate = self;
   
    return headV;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return kImageH(100) ;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.row == 0) {
        if (![FPUserModel ShareInstance].userName) {
            [FPAlertViewShow showAlert:@"请先登录"];
            return;
        }
        FPMyCollectMovieViewController *myCollVC = [[FPMyCollectMovieViewController alloc]init];
        [self.navigationController pushViewController:myCollVC animated:YES];
        
        
        
    }
    else if(indexPath.row == 1)
    {
        if (![FPUserModel ShareInstance].userName) {
        [FPAlertViewShow showAlert:@"请先登录"];
        return;
        }
        FPMyWebCollectController *webColVc = [[FPMyWebCollectController alloc]init];
        [self.navigationController pushViewController:webColVc animated:YES];
    }
    else if (indexPath.row == 2)
    {
        //获取缓存大小字节
        NSUInteger byteSize1= [SDImageCache sharedImageCache].getSize;
        //将字节转为M
        double size = byteSize1/1000/1000;
        NSString *str = [NSString stringWithFormat:@"(%.2fMB)",size];
        
        
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"确认清空?" message:[NSString stringWithFormat:@"当前已缓存%@",str]  delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
        [alert show];
    }
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 1) {
        [[SDImageCache sharedImageCache] clearDisk];
        //创建文件管理者
        NSFileManager *mgr = [NSFileManager defaultManager];
        //获取缓存路径
        NSString *caches = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)lastObject];
        [mgr removeItemAtPath:caches error:nil];

    }
    [UIColor colorWithRed:0.581 green:0.819 blue:0.821 alpha:1.000];
}

#pragma mark 懒加载
-(UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight ) style:UITableViewStyleGrouped];
        [self.view addSubview:_tableView];
    }
    return _tableView;
}
- (UIView *)statusBarView
{
    if (!_statusBarView) {
        _statusBarView = [[UIView alloc]init];
        [self.view addSubview:_statusBarView];
    }
    return _statusBarView;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
